package mmc.server.shell.ops;

import mmc.server.shell.*;

import java.util.Vector;

public class ArgumentList extends Operation {

    protected Vector<Operation> arguments = new Vector<Operation>();

    public ArgumentList(String text) throws ShellException{
        super(text);
        throw new OperationNotAcceptedException(false,"ArgumentLists cannot be instantiated");
    }
    
    /**
     * Creates an ArgumentList with argC arguments out of text. If atLeast is true, more arguments than argC are accepted too.
     * 
     * @param ptext text to parse
     * @param argC Count of args
     * @param atLeast true=(argC or more arguments are accepted) false=(there must be exactly argC arguments)
     * @throws ShellException
     */
    public ArgumentList(String ptext, int argC, boolean atLeast) throws ShellException {
    	this(ptext, argC, atLeast, ",");
    }
    
    public ArgumentList(String ptext, int argC, boolean atLeast, String separator) throws ShellException {
        super(ptext.trim());
        /*
        if(argC<2)
        throw new OperationNotAcceptedException(true, "ArgumentLists with less than 2 arguments are not supported!");
         */
        if (text.trim().startsWith("(") && text.trim().endsWith(")")) {
            text = text.trim().substring(1, text.trim().length() - 1);
            int nextComma = -1;

            if (Shell.OP_PARSE_TRACE > 1) {
                System.out.println("successfully detected a " + this.getClass().toString());
            }

            while ((nextComma = firstPosOfRegex(separator)) != -1) {

                if (Shell.OP_PARSE_TRACE > 0) {
                    System.out.println("argumentlist parsed argument >" + text.substring(0, nextComma) + "<");
                }

                arguments.add(Shell.getOperationTreeFor(text.substring(0, nextComma)));

                text = text.substring(nextComma + 1);
            }

            if(text.trim().length()>0){
            	if (Shell.OP_PARSE_TRACE > 0) {
                    System.out.println("argumentlist parsed argument >" + text + "<");
                }
            	arguments.add(Shell.getOperationTreeFor(text));
            }

            if (arguments.size() < argC) {
                throw new OperationNotAcceptedException(true, "expected " + (atLeast?"at least ":"") + argC + " arguments");
            }
            if (arguments.size() > argC && !atLeast) {
                throw new OperationNotAcceptedException(true, "expected EXACTLY " + argC + " arguments");
            }
        } else {
            throw new OperationNotAcceptedException(false, text + " does not match (  )");
        }
    }

    @Override
    public double execute() {
        throw new Error("ArgumentLists cannot be calculated!");
    }
    
    public Operation getArgument(int index){
        return arguments.get(index);
    }
    
    public double getCalculatedArgument(int index){
        return arguments.get(index).execute();
    }
    
    public Vector<Operation> getArguments(){
        return arguments;
    }
}
